Webster University

COSC 1550 – Introduction to Programming

Test Two

Name  _______________

 

Multiple Choice

Circle the letter of the statement that bests answers/completes each question.  Each question is worth three points.

 

 

1.      Which one of the following statements concerning arithmetic operators is correct?

a)      The division operator is a unary operator.

b)      The division operator applies only to integer values.

c)      The operands of the remainder operator must be integers.

d)      The effects of the unary minus and the addition operators are the same.

 

2.      Which one of the following statements concerning the type of an arithmetic expression is correct?

a)      If even one operand in an arithmetic expression is of type int, the result of the expression is of type int.

b)      If all operands in the expression are of type float, the result is of type float.

c)      If only one operand in a mixed expression is of type float, the resulting type of the expression cannot be determined.

d)      If an arithmetic expression includes the remainder operator, the type of the entire expression is integer.

 

3.      Which one of the following statements concerning the operation of the following if statement is correct?

 

                                    if(A==B)

                                    {

                                                first statements

                                    }

                                    else

                                    {

                                                second statements

                                    }

 

a)      The first statements are executed only if A is equal to B.

b)      The second statements are executed only if A is equal to B.

c)      Both the first statements and the second statements are executed only if A is equal to B.

d)      Both the first statements and the second statements are executed only if A is not equal to B.

 

4.   Which one of the following statements regarding the switch statement is not correct?

      a)   It provides for multiple paths through a program, depending on the value of a variable.

      b)   If the value of the variable is equal to a case value, the statement(s) associated with that value is executed.

      c)   If the value of the variable is not equal to any case value, the statement(s) associated with the default: option is executed.

      d)   If the value of the variable is equal to a case value, but there are no statements associated with that value, no       statements are executed and the switch statement is ended.


5.   The setprecision and setw commands allow the program to

      a)   change the way that the data is stored.

      b)   format the way that data is displayed.

      c)   convert from one the data type to another.

      d)   determine the number of decimal positions used in a calculation.

 

6.   In a conditional expression of an if statement, which one of the following operators is used to represent the logical AND?

      a)   &&

      b)   ||

      c)   ##

      d)   !!

 

7.         The _________ causes a program to wait until information is typed at the keyboard and the Enter key is pressed.

            a.         Input device

            b.         cin object

            c.         cout object

            d.         Preprocessor

 

8.         The character array Company[12] can hold

            a.         Eleven characters and the null terminator

            b.         Thirteen characters

            c.         Twelve characters

            d.         Twelve characters and the null terminator

 

 

9.         When the final value of an expression is assigned to a variable, it will be converted to:

            a.         The smallest possible data type

            b.         The largest possible data type

            c.         The data type of the variable

            d.         The data type of the expression

 

10.       The cin member function, __________, reads an entire line of text, until the [Enter] key is pressed.

            a.         >>

            b.         getline()

            c.         line()

            d.         input()

 

11.       Which of the following will read a string into the array char Names[20] ?

            a.         cin << Names;

            b.         cin Names;

            c.         cin >> Names[20];

            d.         cin >> Names;


12.       When a relational expression is false, it has the value _____.

            a.         one

            b.         zero

            c.         zero, one, or minus one

            d.         less than zero

 

13.       When an if statement is placed within the conditionally-executed code of another if statement, this is known as:

            a.         complexity

            b.         overloading

            c.         nesting

            d.         validation

 

14.       The _____ operator is used in C++ to represent equality.

            a.         =

            b.         ><

            c.         !!

            d.         ==

 

15.       The scope of a local variable is
a)         for the duration of the entire program.
b)         from the time it is defined until the end of the program.
c)         from the time it is defined until the end of the block in which it is defined.
d)         from the beginning of the program until the end of the block in which it is defined.

16.       The statement cout << setw(4) << num4 << "  ";

            a.         Allows four positions for the value in the variable num4

            b.         Outputs "setw(4)"  before the value in the variable num4

            c.         Would use setw(10) to output the value in the variable Num10

            d.         Inputs up to four characters stored in the variable num4

           

 

17.       The stream manipulator, ____________, forces cout to print the digits in fixed-point notation.

            a.         setprecision(2)

            b.         setw(2)

            c.         fixed

            d.         setfixed(2)

 

18.       To write data to a file, you define an object of this data type.

            a.         inputFile

            b.         ifstream

            c.         fstream

            d.         ofstream

 

19.       Which one of the following statements correctly closes the file with a stream name of dataFile?

            a.         dataFile.close();

            b.         close.dataFile();

            c.         dataFile.close;

            d.         close.dataFile;


20.       Assuming outFile is a file stream object and number is a variable, which statement writes the contents of number to the file associated with outFile?

            a.         write(outFile, number);

            b.         outFile >> number;

            c.         outFile << number;

            d.         number >> outFile

 

21.       Which one of the following statements is true regarding the operation of the setprecision stream manipulator?

a.         When used with the fixed stream manipulator, setprecision(3) means 3 significant digits will display.

b.         When used with the fixed stream manipulator, setprecision(3) means 3 decimal digits will display.

c.         When used with the setw stream manipulator, setprecision(3) means 3 significant digits will display.

d.         When used with the setw stream manipulator, setprecision(3) means 3 decimal digits will display.

           

22.       If you intend to place a block of statements within an if statement, you must use __________ around the block.

            a.         parentheses

            b.         square brackets

            c.         quotation marks

            d.         curly braces

           

23.       When a program lets the user know that an invalid choice has been made, this is known as

            a.         input validation

            b.         output correction

            c.         compiler criticism

            d.         output validation

 

24.       The default section of a switch statement performs a similar task as the _______ portion of an if/else if statement.

            a.         conditional

            b.         break

            c.         trailing else

            d.         cast


25.       What will the following program segment display?

 

                        int funny = 7, serious = 15;

                        funny = serious % 2;

                        if (funny != 1)

                        {

                                    funny = 0;

                                    serious = 0;

                        }

                        else if (funny == 2)

                        {

                                    funny = 10;

                                    serious = 10;

                        }

                        else

                        {

                                    funny = 1;

                                    serious = 1;

                        }

                        cout << funny << "   " << serious << endl;

 

a.         7  15

b.         0  0

c.         10  10

d.         1 1

 

26.       What will the following program display?

 

                                    #include <iostream.h>                                      

                                    void main(void)                                                

                                    {                                                                                 

                                         int a = 0, b = 2, x = 4, y = 0;                      

                                         cout << (a == b) << "  ";                            

                                         cout << (a != b) <<  "  ";

                                         cout << (b <=x) << "  ";

                                         cout << (y > a) << endl;

                                    }

            a.  0  1  1  0

            b.  0  0  1  0

            c.  1  1  0  1

            d.  1  0  0  1

 


Problems

            The following questions are problems that require some type of short answer.  Please follow the instructions in each question to complete.  Each answer is worth two points.

 

 

Assume the following declarations in a C++ program:

 

                        int a = 2, b = 3, c = 15, d = 7;

                        float x = -3.5, y=6.5, z = 11.25;

 

What values will be computed by the following arithmetic statements?  (show your work for possible partial credit).

 

            27.       c/d%a

 

 

 

            28.       (c+b)/a-d

 

 

 

            29.       y – z * ( a – b )

 

 

 

            30.       –d * x + y

 

 

 

            31.       static_cast<int>(z)% b * ((1.5 +y) – (z – 0.25))

 

 

 

Find and explain the errors, if any, in the following C++ statements.  Look carefully since these can have multiple errors and the errors can be syntax errors or logic errors.

 

            32.                   if (fee >= $10)

                                        cout << "\nThis is a high fee" << \n;

                                    else

                                         cout << "\nThis fee is ok" << endl

 

 


            33.       switch (Option)

                        {

                                    case ==1:         cout << "Option 1 chosen" << endl;

                                    case ==2:         break;

                                                            cout << "Option 2 chosen" << endl;

                                    case ==3;         cout << "Option 3 chosen" << endl;

                                                            break;

                                    default:             cout << "Not a valid option" <<endl;

                        }

           

 

 

 

 

 

            34.       if (score < 60)

                                    cout << "Your grade is an F\n";

                        if (score < 70)

                                    cout << "Your grade is an D\n";

                        if (score < 80)

                                    cout << "Your grade is an C\n";

                        if (score < 90)

                                    cout << "Your grade is an B\n";

                        else;

                                    cout << "Your grade is an A\n";

 

 

 

                       

           

35.       Given the following C++ code, what will be displayed?

 

            int age = 32;

            char sex = ‘F’;

 

            if (age <= 25 || (age <= 35 && sex == ‘M’))

                        cout << "You are a high risk driver"  << endl;

            else if ((age <=35 && sex == ‘F’) || (age <=45 && sex == ‘M’))

                        cout << "You are a medium risk driver"  << endl;

            else cout << "You are a low risk driver"  << endl;


36.       Given the following C++ code, what will be displayed?

 

            int plan = 2;

 

            cout << "Your insurance plan includes:" << endl;

 

            switch (plan)

            {

                        case 1:              cout << "No deductible" << endl;

                        case 2:              cout << "$10 prescription co-pay" << endl;

                        case 3:              cout << "$20 doctor visit copay" << endl;

                                                              break;

                        default: cout << "No emergency room benefit" << endl;

            }

 

 

 

 

 

37.       Given the following C++ code, what will be displayed?

 

 

            float C = 5;

 

            float F = 9 / 5 * C +32;

 

            cout << "The Fahrenheit value of  " << C << " is " << F << endl;